// vue2 路由配置
// import VueRouter from 'vue-router'
// const router  = new VueRouter({
//   mode: 'history' // hash history,
//   routes: [
//     { path: '/home', component: () => import('@/views/Home/index.vue')}
//   ]
// })
// export default router

// 引入 NProgress 及其样式
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
// 关闭 NProgress 的 loading 效果
NProgress.configure({
  showSpinner: false
})

// vue3 路由配置
import { useUserStore } from '@/stores'
import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  // history: createWebHashHistory(),
  routes: [
    {
      path: '/login',
      component: () => import('@/views/Login/index.vue'),
      meta: { title: '登录' }
    },
    {
      path: '/',
      component: () => import('@/views/Layout/index.vue'),
      redirect: '/home',
      children: [
        {
          path: 'home',
          component: () => import('@/views/Home/index.vue'),
          meta: { title: '首页' }
        },
        {
          path: 'article',
          component: () => import('@/views/Article/index.vue'),
          meta: { title: '健康百科' }
        },
        {
          path: 'notify',
          component: () => import('@/views/Notify/index.vue'),
          meta: { title: '消息通知' }
        },
        {
          path: 'user',
          component: () => import('@/views/User/index.vue'),
          meta: { title: '个人中心' }
        }
      ]
    },
    {
      path: '/user/patient',
      component: () => import('@/views/User/PatientPage.vue'),
      meta: { title: '家庭档案' }
    },
    {
      path: '/consult/fast',
      component: () => import('@/views/Consult/ConsultFast.vue'),
      meta: { title: '选择医院' }
    },
    {
      path: '/consult/dep',
      component: () => import('@/views/Consult/ConsultDep.vue'),
      meta: { title: '选择科室' }
    },
    {
      path: '/consult/illness',
      component: () => import('@/views/Consult/ConsultIllness.vue'),
      meta: { title: '病情描述' }
    }
  ]
})

// 登录访问权限控制
router.beforeEach((to, form) => {
  // 进度条开启
  NProgress.start()
  // 获取用户仓库
  const userStore = useUserStore()
  // 白名单（不登录也能访问的页面）
  const whiteList = ['/login']
  // 没登录，且去的页面还不是白名单页面，跳转到登录页
  if (!userStore.user?.token && !whiteList.includes(to.path)) return '/login'
  // 其他情况，直接放行
})
// 全局后置守卫
router.afterEach((to, form) => {
  document.title = `优医问诊-${to.meta.title}`
  // 进度条关闭
  NProgress.done()
})
export default router
